#include "main.h"

#define NOTE_C4  262    //octave 4 - middle c
#define NOTE_D4  294
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_G4  392
#define NOTE_A4  440
#define NOTE_B4  494
#define NOTE_C5  523

PwmOut buzzer(PC_6);
const int boot_note[] = {NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5};
const int level_note[] = {NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4};
const int note_duration[] = {400, 400, 400, 400, 400, 400, 400, 400,400};
const int boot_note_length =7;
const int level_up_note_length =3;
const int kill_note[] = {NOTE_D4,NOTE_E4,NOTE_B4};
const int key_note[]={NOTE_A4};
const int death_note[] = {NOTE_C5, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_F4, NOTE_E4, NOTE_D4, NOTE_C4};
//
void play_note(int frequency){
    buzzer.period_us((float) 1000000.0f/ (float) frequency);    
    buzzer.pulsewidth_us(buzzer.read_period_us()/2);    //(volume_level*10)                          
}

uint8_t music_flag = 0; //0 no music 1 begin boot music 2 doing music 3 begin key music 4 doing key music 5 begin level up music 6 doing level up music 7 begin kill muisc 8 doing kill music
//9 death music 10 doing death
uint8_t boot_music_index=0;
void boot_music() {
    if (music_flag == 1) {
        flag_ms = 0; 
        music_flag = 2;
    }

    if (music_flag == 2) {
        play_note(boot_note[boot_music_index]);

        // Check if 300ms have passed
        if (flag_ms >= note_duration[boot_music_index]) { 
            boot_music_index++; 
            flag_ms = 0;  
            if (boot_music_index > boot_note_length) {
                music_flag = 0; 
                boot_music_index = 0;
                buzzer.pulsewidth_us(0);
            }
        }
    }
}

void key_feed_back()
{
    if(music_flag == 3){
        flag_ms = 0; 
        music_flag = 4;
    }

    if(music_flag == 4)
    {
        play_note(key_note[0]); 
        if (flag_ms>200) {
            flag_ms = 0;
            music_flag = 0; 
            buzzer.pulsewidth_us(0);
        }
    }
}


void level_up_sound()
{
    if (music_flag == 5) {
        flag_ms = 0; 
        music_flag = 6;
    }

    if (music_flag == 6) {
        play_note(level_note[boot_music_index]);

        // Check if 300ms have passed
        if (flag_ms >= note_duration[boot_music_index]) { 
            boot_music_index++; 
            flag_ms = 0;  
            if (boot_music_index > level_up_note_length) {
                boot_music_index = 0;
                music_flag = 0; 
                buzzer.pulsewidth_us(0);
            }
        }
    }
}


void kill_music()
{
    if(music_flag == 7){
        flag_ms = 0; 
        music_flag = 8;
    }
    if (music_flag == 8) {
        play_note(kill_note[boot_music_index]);
        if (flag_ms >= note_duration[boot_music_index]) { 
            boot_music_index++; 
            flag_ms = 0;  
            if (boot_music_index > 2) {
                boot_music_index = 0;
                music_flag = 0; 
                buzzer.pulsewidth_us(0);
            }
        }
    }
}


void death_music()
{
    if(music_flag == 9){
        flag_ms = 0; 
        music_flag = 10;
    }
    if (music_flag == 10) {
        play_note(death_note[boot_music_index]);
        if (flag_ms >= note_duration[boot_music_index]) { 
            boot_music_index++; 
            flag_ms = 0;  
            if (boot_music_index > boot_note_length) {
                boot_music_index = 0;
                music_flag = 0; 
                buzzer.pulsewidth_us(0);
            }
        }
    }    
}

void buzzer_sound()
{
    boot_music();
    key_feed_back();
    level_up_sound();
    kill_music();
    death_music();
}




